package com.lx.demo.b.utils;

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.MDC;

import java.util.UUID;

/**
 * Created by lanyxp on 2020/9/11
 */
@Slf4j
public final class MDCUtil {
    public static final String THREAD_ID = "THREAD_ID";

    private MDCUtil() {
    }

    public static void put() {
        final String mdc = UUID.randomUUID().toString().replaceAll("-", "");
        if (StringUtils.isNotBlank(MDCUtil.get())) {
            log.info("===>>开启新的MDC old:{},new:{}", MDCUtil.get(), mdc);
            MDCUtil.clear();
            MDC.put(THREAD_ID, mdc);
        }

    }

    public static void put(String clientSerializableNo) {
        MDC.put(THREAD_ID, clientSerializableNo);
    }

    public static String get() {
        return MDC.get(THREAD_ID);
    }

    public static void clear() {
        MDC.clear();
    }
}
